# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
from utils.HttpUtils import HttpUtils


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
微博-粉丝
只能显示5页
https://weibo.com/p/1003061669879400/follow?relate=fans&page=1#Pl_Official_HisRelation__58
"""



class Test_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            # "User-Agent": ua.random
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Cookie": "Ugrow-G0=9ec894e3c5cc0435786b4ee8ec8a55cc; login_sid_t=acde71bba9be8d44a919d08b38036224; cross_origin_proto=SSL; TC-V5-G0=eb26629f4af10d42f0485dca5a8e5e20; WBStorage=42212210b087ca50|undefined; _s_tentry=passport.weibo.com; wb_view_log=1536*8641.25; Apache=426307043156.9882.1581336169937; SINAGLOBAL=426307043156.9882.1581336169937; ULV=1581336169943:1:1:1:426307043156.9882.1581336169937:; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WWyIgAfvDAPGCkEq6N8Kf4q5JpX5K2hUgL.Fo-01h.cehq0eK.2dJLoI0.LxK-LBo5L12qLxKML12eLB-2LxK-L1KeLB.eLxK-L1KeLB.eLxK-L1KBLBKMLxK-L1KBLBKMXeoqt; SSOLoginState=1581336181; SCF=AnhedCeL5mfHtquuyQ3RLmgBKYu5H4Mjtq5S-YtUBcVFV71wi9bOSdNim77kXeBF58KHiQAB4Vn7CdgBhI4C93o.; SUB=_2A25zRTYmDeThGeNN41sX8CjPyjWIHXVQMyDurDV8PUJbmtANLWfRkW9NSZ0OGQ7yhiOWMITInk9RYgqusG262jRs; SUHB=0k1SQ4ewlc2th5; un=koala_sea@sina.com; wvr=6; wb_view_log_5389606319=1536*8641.25; TC-Page-G0=b32a5183aa64e96302acd8febeb88ce4|1581336509|1581336509; webim_unReadCount=%7B%22time%22%3A1581336563266%2C%22dm_pub_total%22%3A1%2C%22chat_group_client%22%3A0%2C%22allcountNum%22%3A48%2C%22msgbox%22%3A0%7D",
            "DNT": "1",
            "Host": "weibo.com",
            "Referer": "https://weibo.com/p/1003061669879400/follow?relate=fans&page=3",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36",
        }

    # 用户粉丝，只能显示5页
    def get_contents(self, current):
        postData = {
        }
        url = f"https://weibo.com/p/1003061669879400/follow?relate=fans&page={current}#Pl_Official_HisRelation__58"
        html = HttpUtils.do_request("GET", url, self.headers, "").text
        user_id = []  # 粉丝ID
        user_name = []  # 粉丝名称
        user_follow = []  # 粉丝的关注
        user_fans = []  # 粉丝的粉丝量
        user_address = []  # 粉丝的地址

        user_id.extend(re.findall(r'<a class=\\"S_txt1\\" target=\\"_blank\\"  usercard=\\"id=(\d+)&refer_flag=\d+_\\" href=\\"\\/\S+\?refer_flag=\d+_\\" >\S+<\\/a>',html))
        user_name.extend(re.findall(r'<a class=\\"S_txt1\\" target=\\"_blank\\"  usercard=\\"id=\d+&refer_flag=\d+_\\" href=\\"\\/\S+\?refer_flag=\d+_\\" >(\S+)<\\/a>',html))
        user_follow.extend(re.findall(r'关注 <em class=\\"count\\"><a target=\\"_blank\\" href=\\"\\/\d+\\/follow\\" >(\d+)<\\/a>',html))
        user_fans.extend(re.findall(r'粉丝<em class=\\"count\\"><a target=\\"_blank\\" href=\\"\\/\d+\\/fans\?current=fans\\" >(\d+)<\\/a>',html))
        user_address.extend(re.findall(r'<em class=\\"tit S_txt2\\">地址<\\/em><span>(\S+\s?\S+?)<\\/span>\\r\\n\\t\\t\\t\\t\\t<\\/div>',html))
        print('user_id', user_id)
        print('user_name', user_name)
        print('user_follow', user_follow)
        print('user_fans', user_fans)
        print('user_address', user_address)


if __name__ == '__main__':
    test_get = Test_Get()
    for i in range(1, 6):
        print(i)
        test_get.get_contents(i)